package com.yoyo.yyadsdk.open;

import android.app.IActivityController;
import android.content.Context;
import android.content.Intent;


import com.anye.greendao.gen.LocalPullEntityDao;
import com.anye.greendao.gen.PullEntityDao;
import com.yoyo.yyadsdk.activity.EmptyActivity;
import com.yoyo.yyadsdk.bean.dao.LocalPullEntity;
import com.yoyo.yyadsdk.bean.dao.PullEntity;
import com.yoyo.yyadsdk.config.PullFeature;
import com.yoyo.yyadsdk.util.ActivityUtils;
import com.yoyo.yyadsdk.util.GreenDaoManager;
import com.yoyo.yyadsdk.util.LogHelper;
import com.yoyo.yyadsdk.util.PullUtils;
import com.yoyo.yyadsdk.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

import static com.yoyo.yyadsdk.util.ActivityUtils.appIsRunning;

/**
 * <permission android:name="android.permission.SET_ACTIVITY_WATCHER" android:protectionLevel="signature" />
 */
public class MyActivityController extends IActivityController.Stub {
    private static final String TAG = "MyActivityController";
    private Context mContext;
    //    /**
//     * 目标(待启动应用)app包名
//     */
//    private String targetPackageName = "com.dollkey.waveview";
//    /**
//     * 观察应用app包名
//     */
//    private String observedPackageName = "com.app.hubert.newbieguide";
    private PullEntityDao pullEntityDao;
    private List<String> tPkgList = new ArrayList<>();
    private boolean isFromSDKLauncher;

    private List<String> killAppPkgList = new ArrayList<>();

    public MyActivityController(Context context) {
        mContext = context;
        pullEntityDao = GreenDaoManager.getInstance().getDaoSession().getPullEntityDao();
        List<PullEntity> pullEntities = pullEntityDao.queryBuilder().where(PullEntityDao.Properties.Type.eq(PullFeature.PULL_LAUNCHER)).list();
        for (PullEntity p : pullEntities)
            if (!tPkgList.contains(p.getTPkg())) {
                tPkgList.add(p.getTPkg());
                LogHelper.e("tPkg:" + p.getTPkg());
            }

    }

    @Override
    public boolean activityStarting(Intent intent, String s) {
        LogHelper.e("s:" + s);

        if (StringUtils.eq(intent.getAction(), Intent.ACTION_MAIN) &&
                intent.getCategories() != null && intent.getCategories().size() > 0 &&
                intent.getCategories().contains(Intent.CATEGORY_HOME)) return true;

        //是否由我方拉起
        if (isFromSDKLauncher) {
            isFromSDKLauncher = false;
//            String action = PullUtils.getAction(s);

            String action = intent.getStringExtra(EmptyActivity.ACTION);
            int pullType = intent.getIntExtra(EmptyActivity.PULL_TYPE, PullFeature.PULL_UN_KNOW);
            LogHelper.e("action:" + action);
            //记录拉活信息到本地
            LocalPullEntityDao localPullEntityDao = GreenDaoManager.getInstance().getDaoSession().getLocalPullEntityDao();
            LocalPullEntity localPullEntity = new LocalPullEntity();
            localPullEntity.setAction(action);
            localPullEntity.setTPkg(s);
            localPullEntity.setType(pullType);
            localPullEntity.setPullTime(System.currentTimeMillis());
            localPullEntityDao.insertOrReplace(localPullEntity);

            List<PullEntity> list = pullEntityDao.queryBuilder().where(PullEntityDao.Properties.Type.eq(pullType),
                    PullEntityDao.Properties.TPkg.eq(s)).list();
            if (list != null && list.size() > 0) {
                PullEntity pullEntity = list.get(0);
                pullEntity.setPullTimes(pullEntity.getPullTimes() + 1);
                pullEntityDao.insertOrReplace(pullEntity);
                if (pullEntity.getAllowDiscover() == 1) {
                    killAppPkgList.add(pullEntity.getTPkg());
                }
            }
            return true;
        }
        //是否是我方的拉起三方页面
        if (intent.getComponent().getClassName().equals(EmptyActivity.class.getName())) {
            isFromSDKLauncher = true;
            return true;
        }

        //是否是监控的app
        if (!tPkgList.contains(s)) return true;
        //判断上次拉活时间
        if (System.currentTimeMillis() - PullFeature.getPullFeatureLastPullTime() < PullFeature.getPullFeatureInterval())
            return true;

        PullEntity pullEntity = PullUtils.getPullEntity(PullFeature.PULL_LAUNCHER);
        if (pullEntity != null && !appIsRunning(mContext, s)) {
            LogHelper.e("PullEntity:" + pullEntity.toString());
            EmptyActivity.launch(mContext, PullFeature.PULL_LAUNCHER, pullEntity);
            return false;
        }
        return true;
    }

    @Override
    public boolean activityResuming(String s) {
        return true;
    }

    @Override
    public boolean appCrashed(String s, int i, String s1, String s2, long l, String s3) {
        return false;
    }

    @Override
    public int appEarlyNotResponding(String s, int i, String s1) {
        return 0;
    }

    @Override
    public int appNotResponding(String s, int i, String s1) {
        return 0;
    }

    @Override
    public int systemNotResponding(String s) {
        return 0;
    }


}
